/*******************************************************************************
*  Project      : Minigate Golfin                                 
*  Program ID   : id_check.js                                 
*  Description  : 아이디, 닉네임 중복검사                                       
*
********************************************************************************
*  Program History                                                            
*  Date        Author    Description                                          
*  ----------  --------  -------------------------------------------------------- 
*  2012-04-16  김효순    Created.  
*  2012-04-18  김효순    Complete.  
*******************************************************************************/

jQuery(function($){
	function clsTitle()
	{
		this._HTMLDom = null;
		this.OBJTITLE = {
			"id": "아이디"
			, "nick": "닉네임"
		};
		this.key = null;
		
		this._init();
	}
	
	clsTitle.prototype = {
		_init: function(){
			var $layer = $("h3:first");
			this._HTMLDom = {
				"$layer": $layer
			}
			
			this.setKey("");
		}
		, setKey: function(key){
			this.key = key;
			this.display();
		}
		, display: function(){
			var $layer = this._HTMLDom["$layer"];
			
			$layer.html(this.OBJTITLE[this.key] + " 중복확인");
		}
	}
	
	// 아이디
	function clsId()
	{
		this._HTMLDom = null;
		
		this._ERRMSG = {
			"NOID": "아이디를 입력해주세요."
			, "INVALID": "아이디는 4~12이하 영문 또는 숫자만 가능합니다."
		}

		this.errMsg = null;
		
		this._init();
	}
	
	clsId.prototype = {
		_init: function(){
			$layer = $("div.pw_area");
			this._HTMLDom = {
				"$layer": $layer
			}
			
			$layer.find("input:text").attr("maxLength", 12);
		}
		, $getLayer: function(){
			return this._HTMLDom["$layer"];
		}
		, getValue: function(){
			var $layer = this._HTMLDom["$layer"];
			
			return $layer.find(":text").val();
		}
		, setValue: function(val){
			var $layer = this._HTMLDom["$layer"];
			
			$layer.find(":text").val(val);
		}
		, isValidate: function(){
			var $layer = this._HTMLDom["$layer"];
			var val = $layer.find(":text").val();
			var reg = /^[a-zA-Z0-9]{4,12}$/ig;
			
			this.errMsg = null;
			if(val.length == 0){
				this.errMsg ="NOID"
				return false;
			}else if(!reg.test(val)){
				this.errMsg = "INVALID";
				return false;
			}
			
			return true;
		}
		, getError: function(){
			if(this.errMsg) return this._ERRMSG[this.errMsg];
			else return null;
		}
	}
	
	function clsNick()
	{
		this._HTMLDom = null;
		
		this._ERRMSG = {
			"NOVALUE": "닉네임을 입력해주세요."
			, "INVALID": "닉네임은 2~6 이하만 가능합니다."
		}

		this.errMsg = null;
		
		this._init();
	}
	
	clsNick.prototype = {
		_init: function(){
			$layer = $("div.pw_area");
			this._HTMLDom = {
				"$layer": $layer
			}
		}
		, $getLayer: clsId.prototype.$getLayer
		, getValue: clsId.prototype.getValue
		, setValue:clsId.prototype.setValue
		, isValidate: function(){
			var $layer = this._HTMLDom["$layer"];
			var val = $layer.find(":text").val();
			
			if(val.length == 0){
				this.errMsg = "NOVALUE";
				return false;
			}else if(val.length < 2 || val.length > 6){
				this.errMsg = "INVALID"
				return false;
			}
			
			return true;
		}, getError: clsId.prototype.getError
	}
	
	function clsDisplay()
	{
		this._HTMLDom = null;
		this.infoData = null;
		
		this._init();
	}
	
	clsDisplay.prototype = {
		_init: function(){
			$layer = $("div.pw_area");
			this._HTMLDom = {
				"$layer": $layer
			}
			
			this.infoData = {
				"사용가능여부": null
				, "메시지": null
			}
			
			$layer.find("p:gt(0)").hide();
			this.display();
		}
		, setInfo: function(thisData){
			this.infoData = thisData;
			this.display();
		}
		, display: function(){
			var $layer = this._HTMLDom["$layer"];
			
			$layer.find("p:gt(0)").hide();
			
			if(this.infoData["메시지"]){
				$layer.find("p:eq(1)").show().html(this.infoData["메시지"]);
			}
			
			if(this.infoData["사용가능여부"]){
				$layer.find("p:eq(2)").show();
			}
			
			setTimeout(function(){
				_util.setWindowSize();
			}, 500);
		}
	}
	
	function clsComm()
	{
		this.checkValue = null;
		
		this._init();
	}
	
	clsComm.prototype = {
		_init: function(){

		}
		, idSubmit: function(val){
			var thisClass = this;
			
			this.checkValue =val;

			//ajax
			var serviceName = "checkIdDuplication";
			document.objGlobal.ajax({
				"sid":"US001",
				"servicename":serviceName,
				"listcount":10,
				"currpageno":1
			}, {
				"userId": val
			}, function(ret){
				document.objControl.finishIdSubmit({
					"결과코드": ret["msp"]["contents"]["serviceResult"]
					, "메시지": document.objGlobal.getResultMsg(ret)
				});
			});
		}
		, nickSubmit: function(val){
			var thisClass = this;

			this.checkValue = val;
			
			//ajax
			var serviceName = "checkNicknameDuplication";
			document.objGlobal.ajax({
				"sid":"US001",
				"servicename":serviceName,
				"listcount":10,
				"currpageno":1
			}, {
				"nickname": val
			}, function(ret){
				document.objControl.finishNickSubmit({
					"결과코드": ret["msp"]["contents"]["serviceResult"]
					, "메시지": _gf.getResultMsg(ret)
				});
			});
		}
		, getCheckValue: function(){
			return this.checkValue;
		}
	}
	
	
	function clsControl()
	{
		this.objComm = null;
		this.obj = null;
		this.objTitle = null;
		this.objDisplay = null;
		this.openerControl = null;
		
		this.q_type = null;
		
		this._permitStart = null;
	}
	
	clsControl.prototype = {
		_init: function(){
			this._preload();
			
			this.objComm = new clsComm();
			this.objTitle = new clsTitle();
			this.objDisplay = new clsDisplay();
			
			this._beforeStart();
			this._bind();
		}
		, _preload: function(){
			_gf.spinStart();
			
			this.q_type = _util.getParam("q_type");
			
			if(opener){
				if(opener.document.objControl){
					this.openerControl = opener.document.objControl;
				}
			}
			
			//this.obj = new clsNick();
			
			if(!this.openerControl){
				self.close();
				return;
			}
			
			switch(this.q_type){
				case "id": 
					this.obj = new clsId();
					break;
				case "nick":
					this.obj = new clsNick();
					break;
			}
		}
		, _beforeStart: function(){
			var thisClass = this;
			
			this._permitStart = {
				
			}
			
			_util.executeThread(function(){
				for(var key in thisClass._permitStart){
					if(!thisClass._permitStart[key]) return false;
				}
				
				return true;
			}, function(){
				_gf.spinEnd();
				thisClass._start();
			});
		}
		, _bind: function(){
			var thisClass = this;
			
			this.obj.$getLayer().on("keydown", "input:text", function(e){
				e = e || event;
				
				if(e.keyCode ==13) thisClass.submit();
			});
			
			// 중복확인
			this.obj.$getLayer().on("click", "input:image", function(){
				thisClass.submit();
			});
			
			// 사용하기
			this.obj.$getLayer().on("click", "img[alt='사용하기']", function(){
				if(thisClass.openerControl) thisClass.openerControl.returnCheck(window, thisClass.q_type, thisClass.objComm.getCheckValue());
				self.close();
			});
			
			// 닫기버튼
			$("p.pop_close").find("img").css("cusor", "pointer").click(function(){
				self.close();
			});
		}
		, _start: function(){
			var nickValue = this.openerControl.getValue(this.q_type);
			//nickValue = "골프인";
			
			this.objTitle.setKey(this.q_type);
			this.obj.setValue(nickValue);
			this.submit();
		}
		// 중복확인
		, submit: function(){
			if(!this.obj.isValidate()){
				this.objDisplay.setInfo({
					"사용가능여부": false
					, "메시지": this.obj.getError()
				});
				return;
			}
			
			_gf.spinStart("searching...");
			
			switch(this.q_type){
				case "id":
					this.objComm.idSubmit(this.obj.getValue());
					break;
				case "nick": 
					this.objComm.nickSubmit(this.obj.getValue());
					break;
			}
		}
		// 아이디 조회완료
		, finishIdSubmit: function(ret){
			_gf.spinEnd();
			
			if(ret["결과코드"] == "00002"){
				if(confirm(ret["메시지"])){
					document.location.href = "pop_together.html?q_id=" + this.obj.getValue();
				}
				return;
			}
			
			this.objDisplay.setInfo({
				"사용가능여부": (ret["결과코드"] == "00000")? true:false
				, "메시지": ret["메시지"]
			});
		}
		// 닉네임 조회완료
		, finishNickSubmit: function(ret){
			_gf.spinEnd();
			
			this.objDisplay.setInfo({
				"사용가능여부": (ret["결과코드"] == "00000")? true:false
				, "메시지": ret["메시지"]
			});
		}
	}
	
	document.objControl = new clsControl();
	document.objControl._init();
});